﻿using RSFrame.Services;
using RSModels;
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace CompanyService
{
    class CompanyService : ICompanyService
    {
        public IDBService DBService { get; set; }

        public CompanyService(IDBService dBService)
        {
            DBService = dBService;
        }

        public List<Company> AllAuthorizedCompanies
        {
            get
            {
                try
                {
                    List<Company> result = new();
                    DbConnection c = DBService.EstablishConnection();
                    DbDataReader a = DBService.ReadData(c, "exec dbo.get_all_companies",
                        new Dictionary<string, object> { });
                    while (a.Read())
                    {
                        result.Add(new Company()
                        {
                            ID = Convert.ToInt32(a[0]),
                            Name = (string)a[1],
                            Description = (string)a[2],
                            AuthorizedTime = (string)a[3],
                            CompanyPosition = new Position() { Lng = (float)Convert.ToDouble(a[4]), Lat = (float)Convert.ToDouble(a[5]) },
                            CompanyType = (CompanyType)Convert.ToInt32(a[6]),
                        });
                    }
                    DBService.CloseConnection(c);
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        public List<Company> TransportationCompanies => throw new NotImplementedException();

        public List<Company> TreatmentCompanies => throw new NotImplementedException();

        public List<Company> TransferCompanies => throw new NotImplementedException();

        public int? AddCompany(string name, string des, int type, float lng, float lat, int userAccount, string Password)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                object Result = DBService.AddData(c, "exec add_company @name=@_name,@des=@_des,@atime=@_atime,@lng=@_lng,@lat=@_lat,@type=@_type", new Dictionary<string, object>
                {
                    { "@_name", name },
                    { "@_des", des },
                    { "@_atime", DateTime.Now.ToString() },
                    { "@_lng", lng},
                    { "@_lat", lat },
                    { "@_type", type }
                });
                DBService.CloseConnection(c);
                return Convert.ToInt32(Result);
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteCompany(int companyID, int userAccount, string Password)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                int a = DBService.WriteData(c, "exec dbo.del_company @id=@_id", new Dictionary<string, object>
                { { "@_id", companyID } });
                DBService.CloseConnection(c);
                if (a == 1)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        public bool EditCompany(int companyID, Company newCompanyInfo)
        {
            throw new NotImplementedException();
        }

        public List<Company> SearchCompanyByID(int id)
        {
            try
            {
                List<Company> result = new();
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec dbo.get_companies_by_id @ID=@_id",
                    new Dictionary<string, object> { { "@_id", id } });
                while (a.Read())
                {
                    result.Add(new Company()
                    {
                        ID = Convert.ToInt32(a[0]),
                        Name = (string)a[1],
                        Description = (string)a[2],
                        AuthorizedTime = (string)a[3],
                        CompanyPosition = new Position() { Lng = (float)Convert.ToDouble(a[4]), Lat = (float)Convert.ToDouble(a[5]) },
                        CompanyType = (CompanyType)Convert.ToInt32(a[6]),
                    });
                }
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }

        public List<Company> SearchCompanyByName(string keyword)
        {
            try
            {
                List<Company> temp = AllAuthorizedCompanies;
                List<Company> result = new();
                if (temp != null)
                {
                    foreach (var item in temp)
                    {
                        if (item.Name.Contains(keyword))
                            result.Add(item);
                    }
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
    }
}
